﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using StockFinder.Model;

namespace StockFinder.DataAccess
{
    public class PriceLoadRunDataAccess : DataAccessBase
    {
        private const string INSERT_PRICELOADRUN_COMMAND = "dbo.InsertPriceLoadRun";
        private const string INSERT_PRICELOADRUNRESULT_COMMAND = "dbo.InsertPriceLoadRunResult";

        public void InsertPriceLoadRun(PriceLoadRun priceLoadRun)
        {
            SqlConnection connection = CreateOpenConnection();

            using (SqlCommand command = new SqlCommand(INSERT_PRICELOADRUN_COMMAND, connection) { CommandType = System.Data.CommandType.StoredProcedure })
            {
                command.Parameters.AddWithValue("@RunDate", priceLoadRun.RunDate);
                command.Parameters.AddWithValue("@DateFrom", priceLoadRun.DateFrom);
                command.Parameters.AddWithValue("@DateTo", priceLoadRun.DateTo);

                command.Parameters.Add(new SqlParameter() { ParameterName = "RETURN_VALUE", Direction = System.Data.ParameterDirection.ReturnValue });

                command.ExecuteNonQuery();

                int result = (int)command.Parameters["RETURN_VALUE"].Value;

                priceLoadRun.PriceLoadRunId = result;
            }
        }

        public void InsertPriceLoadRunResult(PriceLoadRunResult priceLoadRunResult)
        {
            SqlConnection connection = CreateOpenConnection();

            using (SqlCommand command = new SqlCommand(INSERT_PRICELOADRUNRESULT_COMMAND, connection) { CommandType = System.Data.CommandType.StoredProcedure })
            {
                command.Parameters.AddWithValue("@PriceLoadRunId", priceLoadRunResult.PriceLoadRun.PriceLoadRunId);
                command.Parameters.AddWithValue("@SymbolId", priceLoadRunResult.Symbol.SymbolId);
                command.Parameters.AddWithValue("@Result", priceLoadRunResult.Result);

                command.ExecuteNonQuery();
            }
        }
    }
}
